package lt.hsu.steam;

import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SteamKit {


    public static <S, T> List<T> toList(List<S> source, Function<S, T> f) {
        Assert.isTrue(source != null && !source.isEmpty(), "source must not be null or empty");
        Assert.isTrue(f != null, "conversion function must not be null");
        return source.stream().map(f).collect(Collectors.toList());
    }


    public static <S, K, V> Map<K, V> toMap(List<S> source, Function<S, K> kf, Function<S, V> vf) {
        //断言：source参数不能为null或者为空
        Assert.isTrue(source != null && !source.isEmpty(), "source must not be null or empty");
        Assert.isTrue(kf != null && vf != null, "conversion function must not be null or empty");
        //将source列表转换为stream，并使用collect操作将stream转换为Map，其中key为kf函数处理后的结果，value为vf函数处理后的结果
        return source.stream().collect(Collectors.toMap(kf, vf, (o1, o2) -> o1));
    }
}
